﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Configuration;
using System.Web.Security;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Collections;
using System.IO;
using System.Drawing;
using System.Text;
using System.Net.Mail;
using System.Net;
using System.IO;
using System.Text;


public partial class shenhe1 : System.Web.UI.Page
{
    static bool yishenhe = false;
    string mail;
    string project;
    string Dxiao;
    string Dshen1;
    string Dshen2;
    protected void Page_Load(object sender, EventArgs e)
    {

        if (Session["name"] == null)
        {
            Response.Redirect("~/error.aspx?");
        }

        string JudgePower = Session["power"].ToString();
        if (JudgePower != "超级管理员" && JudgePower != "销售" && JudgePower != "主管")
        {

            Response.Redirect("~/PowerError.aspx?");
        }
        if (!IsPostBack)
        {
            this.bind();
        }
    }

    public SqlConnection GetConnection()
    {
        string myStr = ConfigurationManager.AppSettings["ConnectionString"].ToString();
        SqlConnection myConn = new SqlConnection(myStr);
        return myConn;
    }

    protected void bind()
    {
        SqlConnection myConn = GetConnection();
        myConn.Open();

        string sql1 = "select UName from Users where UPower='主管'";
        SqlDataAdapter sqlDa = new SqlDataAdapter(sql1, myConn);
        DataSet sqlDs = new DataSet();
        sqlDa.Fill(sqlDs);
        DropDownList1.DataSource = sqlDs;
        DropDownList1.DataTextField = "UName";
        DropDownList1.DataBind();
        this.DropDownList1.Items.Insert(0, new ListItem("------------", "------------"));


        string sql = "select * from Dan where ID='" + Session["SHID"].ToString() + "'";
        SqlCommand sqlcmd = new SqlCommand(sql, myConn);
        sqlcmd.ExecuteNonQuery();
        SqlDataReader read = sqlcmd.ExecuteReader();
        read.Read();
        string PASS = read["DISPass"].ToString();
        string SHENHE = read["DShen1"].ToString();
        read.Close();

        string sqls = "select * from Users where UName='" + SHENHE + "'";
        SqlCommand cmds = new SqlCommand(sqls, myConn);
        cmds.ExecuteNonQuery();
        SqlDataReader reads = cmds.ExecuteReader();
        reads.Read();
        string POWER = reads["UPower"].ToString();
        reads.Close();

        myConn.Close();

        if (yishenhe == false)
        {
            if (PASS == "未审核")
            {
                if (POWER == "主管")
                {
                    Label1.Visible = false;
                    DropDownList1.Visible = false;
                    Button1.Visible = false;
                    Button2.Visible = false;
                    Button3.Visible = true;
                    Button4.Visible = true;
                    GridView2.Visible = true;
                    Label2.Visible = true;
                }
                else
                {
                    Label1.Visible = true;
                    DropDownList1.Visible = true;
                    Button1.Visible = true;
                    Button2.Visible = true;
                    Button3.Visible = false;
                    Button4.Visible = false;
                    GridView2.Visible = true;
                    Label2.Visible = true;
                }
            }
            else if (PASS == "二级审核通过")
            {
                Label1.Visible = false;
                DropDownList1.Visible = false;
                Button1.Visible = false;
                Button2.Visible = false;
                Button3.Visible = true;
                Button4.Visible = true;
                GridView2.Visible = true;
                Label2.Visible = true;
            }
        }
        else
        {

            Label1.Visible = false;
            DropDownList1.Visible = false;
            Button1.Visible = false;
            Button2.Visible = false;
            Button3.Visible = false;
            Button4.Visible = false;
            GridView2.Visible = true;
            Label2.Visible = true;

        }

        dingdan();


        myConn.Close();
    }

    protected void dingdan()
    {
        SqlConnection myConn = GetConnection();
        myConn.Open();

        string sqlStr = "select RGID as 货品编号,GTYPEName as 类别,GCHName as 名称,GXinhao as 型号,GBrand as 品牌,  RWID as 仓库编号,RQuantity as 数量,PQuantity as 项目库存,bianhao as 项目编号,Projects.PName as 项目名  from RecordsOUT, Goods,Projects,ProjectsQuantity where RecordsOUT.RGID=Goods.GID and RecordsOUT.RGID=ProjectsQuantity.PGID and RecordsOUT.RPID=Projects.PID and  RecordsOUT.RPID=ProjectsQuantity.PID and  RecordsOUT.RDID='" + Session["SHID"].ToString() + "' order by RID desc";
        SqlDataAdapter myDa = new SqlDataAdapter(sqlStr, myConn);
        DataSet myDs = new DataSet();
        myDa.Fill(myDs);
        GridView2.DataSource = myDs;
        GridView2.DataKeyNames = new string[] { "货品编号" };
        GridView2.DataBind();
        myDa.Dispose();
        myDs.Dispose();

        myConn.Close();
    }

    protected bool panduan()
    {
        bool fanhui = true;
        SqlConnection myConn = GetConnection();
        myConn.Open();
        string sqlStr = "select RID from RecordsOUT where RDID='" + Session["SHID"].ToString() + "' order by RID desc";
        SqlCommand comm = new SqlCommand(sqlStr, myConn);
        comm.ExecuteNonQuery();
        SqlDataReader read = comm.ExecuteReader();
        while (read.Read())
        {
            string RID = read["RID"].ToString();
            if (!oneortwo(Convert.ToInt32(RID)))
            {
                fanhui = false;
            }
        }
        read.Close();
        myConn.Close();
        return fanhui;
    }

    protected void notpass(int sum, int rpid, int rgid, int rwid, string AOG)
    {
        string sqll;
        if (AOG == "审核未通过")
        {
            SqlConnection myConn = GetConnection();
            myConn.Open();


            sqll = "select PQuantity from ProjectsQuantity where PID='" + rpid + "' and PGID='" + rgid + "'";
            SqlCommand com = new SqlCommand(sqll, myConn);
            com.ExecuteNonQuery();
            SqlDataReader Pread = com.ExecuteReader();
            Pread.Read();
            string QIAN = Pread["PQuantity"].ToString();
            Pread.Close();

            string sql = "update  ProjectsQuantity set PQuantity=PQuantity + '" + sum + "' where PID='" + rpid + "' and PGID='" + rgid + "'";
            SqlCommand sqlcmd = new SqlCommand(sql, myConn);
            sqlcmd.ExecuteNonQuery();

            sqll = "select PQuantity from ProjectsQuantity where PID='" + rpid + "' and PGID='" + rgid + "'";
            com = new SqlCommand(sqll, myConn);
            com.ExecuteNonQuery();
            SqlDataReader sqlread = com.ExecuteReader();
            sqlread.Read();
            string HOU = sqlread["PQuantity"].ToString();
            sqlread.Close();

            if (Convert.ToInt32(HOU) > 0)
            {
                if (Convert.ToInt32(QIAN) < 0)
                {
                    sqll = "update  ProjectsQuantity set PQuantity=PQuantity-'" + Convert.ToDouble(QIAN) + "'where PID='6' and PGID='" + rgid + "'";
                    com = new SqlCommand(sqll, myConn);
                    com.ExecuteNonQuery();
                }
            }
            else
            {
                sqll = "update  ProjectsQuantity set PQuantity=PQuantity+'" + Convert.ToDouble(sum) + "'where PID='6' and PGID='" + rgid + "'";
                com = new SqlCommand(sqll, myConn);
                com.ExecuteNonQuery();
            }

            string sqlupdate = "update Store set SQuantity=SQuantity + '" + sum + "' where SGID='" + rgid + "' and SWID='" + rwid + "'";
            sqlcmd.CommandText = sqlupdate;
            sqlcmd.ExecuteNonQuery();
            myConn.Close();
        }
        else if (AOG == "补货未通过")
        {
            SqlConnection myConn = GetConnection();
            myConn.Open();
            string sql = "update  ProjectsQuantity set PQuantity=PQuantity - '" + sum + "' where PID='" + rpid + "' and PGID='" + rgid + "'";
            SqlCommand sqlcmd = new SqlCommand(sql, myConn);
            sqlcmd.ExecuteNonQuery();
            string sqlupdate = "update ProjectsQuantity set PQuantity=PQuantity + '" + sum + "' where PID='6' and PGID='" + rgid + "'";
            sqlcmd.CommandText = sqlupdate;
            sqlcmd.ExecuteNonQuery();
            myConn.Close();

        }
    }


    protected bool oneortwo(int rid)
    {
        SqlConnection myConn = GetConnection();
        myConn.Open();
        string sql = "select * from RecordsOUT where RID='" + rid + "'";
        SqlCommand comm = new SqlCommand(sql, myConn);
        comm.ExecuteNonQuery();
        SqlDataReader read = comm.ExecuteReader();
        read.Read();
        string GID = read["RGID"].ToString();
        string PID = read["RPID"].ToString();
        string sum = read["RQuantity"].ToString();
        read.Close();

        string sqlsum = "select PQuantity from ProjectsQuantity where PGID='" + GID + "' and PID='" + PID + "'";
        SqlCommand comsum = new SqlCommand(sqlsum, myConn);
        comsum.ExecuteNonQuery();
        SqlDataReader readsum = comsum.ExecuteReader();
        readsum.Read();
        string SUM = readsum["PQuantity"].ToString();
        readsum.Close();
        myConn.Close();

        if (Convert.ToInt32(SUM) < 0)
        {
            return false;
        }
        else
            return true;
    }


    protected void Button1_Click(object sender, EventArgs e)
    {
        if (DropDownList1.SelectedValue == "------------")
        {
            if (panduan())
            {
                SqlConnection myConn = GetConnection();
                myConn.Open();
                string sql = "update Dan set DISPass='已通过' where id='" + Session["SHID"].ToString() + "'";
                SqlCommand sqlcmd = new SqlCommand(sql, myConn);
                sqlcmd.ExecuteNonQuery();
                string sqlupdate = "update RecordsOUT set RifAOG='待发货' where RDID='" + Session["SHID"].ToString() + "'";
                sqlcmd.CommandText = sqlupdate;
                sqlcmd.ExecuteNonQuery();
                myConn.Close();
                name();
                for (int i = 0; i < 4; i++)
                    SendMail(i);
                Response.Redirect("~/success.aspx?");
            }
            Response.Write("<script>alert('此出货单需要填写二级审核人!')</script>");
        }
        else
        {
            SqlConnection myConn = GetConnection();
            myConn.Open();
            string sql = "update Dan set Dshen2='" + DropDownList1.SelectedValue + "', DISPass='二级审核通过' where id='" + Session["SHID"].ToString() + "'";
            SqlCommand sqlcmd = new SqlCommand(sql, myConn);
            sqlcmd.ExecuteNonQuery();
            myConn.Close();
            SendMail();
            Response.Redirect("~/success.aspx?");
        }
    }

    protected void Button2_Click(object sender, EventArgs e)
    {
        SqlConnection myConn = GetConnection();
        myConn.Open();
        string sql = "update Dan set DISPass='未通过' where id='" + Session["SHID"].ToString() + "'";
        SqlCommand sqlcmd = new SqlCommand(sql, myConn);
        sqlcmd.ExecuteNonQuery();
        string sqlout = "update RecordsOUT set RifAOG='审核未通过' where RDID='" + Session["SHID"].ToString() + "'";
        sqlcmd.CommandText = sqlout;
        sqlcmd.ExecuteNonQuery();
        string sqlupdate = "select * from RecordsOUT where RDID='" + Session["SHID"].ToString() + "'";
        SqlCommand sqlud = new SqlCommand(sqlupdate, myConn);
        sqlud.ExecuteNonQuery();
        SqlDataReader sqlrd = sqlud.ExecuteReader();
        while (sqlrd.Read())
        {
            notpass(Convert.ToInt32(sqlrd["RQuantity"]), Convert.ToInt32(sqlrd["RPID"]), Convert.ToInt32(sqlrd["RGID"]), Convert.ToInt32(sqlrd["RWID"]), sqlrd["RifAOG"].ToString());
        }
        sqlrd.Close();
        myConn.Close();
        name();
        SendMailFail();
        Response.Redirect("~/success.aspx?");
    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        SqlConnection myConn = GetConnection();
        myConn.Open();
        string sql = "update Dan set DISPass='已通过' where id='" + Session["SHID"].ToString() + "'";
        SqlCommand sqlcmd = new SqlCommand(sql, myConn);
        sqlcmd.ExecuteNonQuery();
        string sqlupdate = "update RecordsOUT set RifAOG='待发货' where RDID='" + Session["SHID"].ToString() + "'";
        sqlcmd.CommandText = sqlupdate;
        sqlcmd.ExecuteNonQuery();
        myConn.Close();
        name();
        for (int i = 0; i < 4; i++)
            SendMail(i);
        Response.Redirect("~/success.aspx?");
    }
    protected void Button4_Click(object sender, EventArgs e)
    {
        string sqlupdate = "select * from RecordsOUT where RDID='" + Session["SHID"].ToString() + "'";
        SqlConnection myConn = GetConnection();
        myConn.Open();
        string sql = "update Dan set DISPass='未通过' where id='" + Session["SHID"].ToString() + "'";
        SqlCommand sqlcmd = new SqlCommand(sql, myConn);
        sqlcmd.ExecuteNonQuery();
        //string sqloutBH2 = "select ID as 编号 , DName as 购货单名称,DDes as 类别 ";
        // string sqlStr = "select ID as 编号 , DName as 购货单名称,DDes as 类别,PName as 所属项目,DXiao as 销售 from [RecordsOUT],[Projects],[Dan] where  Dan.DPID=Projects.PID and (( DDes='出货单' ) or ( DDes='补货单' )) and ((DISPass='未审核' and DShen1='" + Session["name"].ToString() + "' ) or ( DISPass='二级审核通过' and DShen2='" + Session["name"].ToString() + "')) and RDID='" + GridView1.SelectedValue + "'";
        //SqlCommand sqlout2 = new SqlCommand(sqlupdate, myConn);
        //sqlout2.ExecuteNonQuery();
        sqlcmd.CommandText = sqlupdate;
        sqlcmd.ExecuteNonQuery();
        SqlDataReader sqlrd2 = sqlcmd.ExecuteReader();
        sqlrd2.Read();
        string dname = sqlrd2["RDate"].ToString();
        string sqlout = "";
        string zhuangtai = sqlrd2["RifAOG"].ToString();
        if (sqlrd2["RifAOG"].ToString() == "补货未审核")
        {
            sqlout = "update RecordsOUT set RifAOG='补货未通过' where RDID='" + Session["SHID"].ToString() + "'";
        }
        else
        {
            sqlout = "update RecordsOUT set RifAOG='审核未通过' where RDID='" + Session["SHID"].ToString() + "'";
        }
        sqlrd2.Close();
        sqlcmd.CommandText = sqlout;
        sqlcmd.ExecuteNonQuery();
        // string sqlupdate = "select * from RecordsOUT where RDID='" + GridView1.SelectedValue + "'";
        SqlCommand sqlud = new SqlCommand(sqlupdate, myConn);
        sqlud.ExecuteNonQuery();
        SqlDataReader sqlrd = sqlud.ExecuteReader();
        while (sqlrd.Read())
        {
            notpass(Convert.ToInt32(sqlrd["RQuantity"]), Convert.ToInt32(sqlrd["RPID"]), Convert.ToInt32(sqlrd["RGID"]), Convert.ToInt32(sqlrd["RWID"]), sqlrd["RifAOG"].ToString());
        }
        sqlrd.Close();
        myConn.Close();
        name();
        SendMailFail();
        Response.Redirect("~/success.aspx?");
    }

    public void SendMail()
    {
        SqlConnection myConn = GetConnection();
        myConn.Open();
        string sqlStr = "select UID,UName from [Users] where UName='" + DropDownList1.SelectedValue + "' ";
        SqlCommand cmd = new SqlCommand(sqlStr, myConn);
        cmd.ExecuteNonQuery();
        SqlDataReader read = cmd.ExecuteReader();
        read.Read();
        mail = read["UID"].ToString() + "@enercomn.com";
        read.Close();
        sqlStr = "select ID   ,PName  from [Projects],[Dan] where  Dan.DPID=Projects.PID and ID='" + Session["SHID"].ToString() + "' ";
        cmd = new SqlCommand(sqlStr, myConn);
        cmd.ExecuteNonQuery();
        read = cmd.ExecuteReader();
        read.Read();
        string project = read["PName"].ToString();
        read.Close();
        mail = "xuchaoyi@enercomn.com";

        MailMessage message = new MailMessage();

        message.To.Add(mail);
        message.From = new MailAddress("research@enercomn.com", "仓库管理系统", Encoding.UTF8);
        message.Subject = "您收到" + project + "项目的出货申请!";
        message.SubjectEncoding = Encoding.UTF8;
        message.Body = "详情请点击 http://www.enercomn.com/warehouse";
        message.BodyEncoding = Encoding.UTF8;
        message.IsBodyHtml = false;
        message.Priority = MailPriority.Normal;
        SmtpClient smtp = new SmtpClient();
        smtp.Credentials = new System.Net.NetworkCredential("research@enercomn.com", "111111");
        smtp.Host = "smtp.ym.163.com";
        object userState = message;


        try
        {
            smtp.SendAsync(message, userState);

        }
        catch (Exception ex)
        {
            //MessageBox.Show(ex.Message, "Tips");
            Response.Write("<script>alert('" + ex.Message + "')</script>");
        }
    }

    public void SendMail(int i)
    {


        MailMessage message = new MailMessage();
        string[] arr = new string[20];
        arr[0] = "xuchaoyi@enercomn.com";
        // arr[1] = Dxiao + "@enercomn.com";
        arr[1] = "xuchaoyi@enercomn.com";
        arr[2] = "xuchaoyi@enercomn.com";
        // arr[2] = Dshen1 + "@enercomn.com";
        arr[3] = Dshen2 + "@enercomn.com";
        //  arr[0] = "zhengboyi@enercomn.com";
        message.To.Add(arr[i]);
        message.From = new MailAddress("research@enercomn.com", "仓库管理系统", Encoding.UTF8);
        message.Subject = project + "项目的出货申请已通过审核!";
        message.SubjectEncoding = Encoding.UTF8;
        message.Body = "详情请点击 http://www.enercomn.com/warehouse";
        message.BodyEncoding = Encoding.UTF8;
        message.IsBodyHtml = false;
        message.Priority = MailPriority.Normal;
        SmtpClient smtp = new SmtpClient();
        smtp.Credentials = new System.Net.NetworkCredential("research@enercomn.com", "111111");
        smtp.Host = "smtp.ym.163.com";
        object userState = message;


        try
        {
            smtp.SendAsync(message, userState);

        }
        catch (Exception ex)
        {
            //MessageBox.Show(ex.Message, "Tips");
            Response.Write("<script>alert('" + ex.Message + "')</script>");
        }
    }
    public void name()
    {
        SqlConnection myConn = GetConnection();
        myConn.Open();
        string sqlStr = "select ID   ,PName,Dshen1,Dshen2,Dxiao  from [Projects],[Dan] where  Dan.DPID=Projects.PID and ID='" + Session["SHID"].ToString() + "' ";
        SqlCommand cmd = new SqlCommand(sqlStr, myConn);
        cmd.ExecuteNonQuery();
        SqlDataReader read = cmd.ExecuteReader();
        read.Read();
        project = read["PName"].ToString();
        Dxiao = read["Dxiao"].ToString();
        Dshen1 = read["Dshen1"].ToString();
        Dshen2 = read["Dshen2"].ToString();
        read.Close();
        sqlStr = "select UID,UName from [Users] where UName='" + Dshen1 + "' ";
        cmd = new SqlCommand(sqlStr, myConn);
        cmd.ExecuteNonQuery();
        read = cmd.ExecuteReader();
        read.Read();
        Dshen1 = read["UID"].ToString();
        read.Close();
        if (Dshen2 != "")
        {
            sqlStr = "select UID,UName from [Users] where UName='" + Dshen2 + "' ";
            cmd = new SqlCommand(sqlStr, myConn);
            cmd.ExecuteNonQuery();
            read = cmd.ExecuteReader();
            read.Read();
            Dshen2 = read["UID"].ToString();
            read.Close();
        }
        else
        {
            Dshen2 = "admin";
        }
        mail = "xuchaoyi@enercomn.com";

    }

    public void SendMailFail()
    {
        //mail = Dxiao + "@enercomn.com";
        mail = "xuchaoyi@enercomn.com";

        MailMessage message = new MailMessage();

        message.To.Add(mail);
        message.From = new MailAddress("research@enercomn.com", "仓库管理系统", Encoding.UTF8);
        message.Subject = "" + project + "项目的出货申请没有通过!";
        message.SubjectEncoding = Encoding.UTF8;
        message.Body = "详情请点击 http://www.enercomn.com/warehouse";
        message.BodyEncoding = Encoding.UTF8;
        message.IsBodyHtml = false;
        message.Priority = MailPriority.Normal;
        SmtpClient smtp = new SmtpClient();
        smtp.Credentials = new System.Net.NetworkCredential("research@enercomn.com", "111111");
        smtp.Host = "smtp.ym.163.com";
        object userState = message;


        try
        {
            smtp.SendAsync(message, userState);

        }
        catch (Exception ex)
        {
            //MessageBox.Show(ex.Message, "Tips");
            Response.Write("<script>alert('" + ex.Message + "')</script>");
        }
    }
}